package com.lsh.dao.advices;

import com.lsh.domain.AdviceComment;
import com.lsh.domain.UserComment;
import com.lsh.domain.UserReply;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import javax.crypto.Cipher;
import javax.print.DocFlavor;
import java.util.List;

/**
 * 消息-评价管理模块
 * 持久层接口
 * @author Administrator
 * @date 2019/10/29 9:29
 */
@Repository
public interface AdviceCommentDao {

    // 更新当前用户所有收到评价的已读情况
    @Update("UPDATE user_comments" +
            " SET is_read = 1" +
            " WHERE artid IN (SELECT id FROM user_articles WHERE uid = #{userId})")
    void updateIsReadByUserId(String userId);

    // 查询当前用户所有未读的评价记录数
    @Select("SELECT count(*) AS unread_comment_count" +
            " FROM user_comments c" +
            " LEFT JOIN user_articles a" +
            " ON c.artid = a.id" +
            " WHERE a.uid = #{userId} AND c.is_read = 0")
    int findUnReadCommentCount(String userId);

    // 获取当前用户所有收到评价的信息
    @Select("SELECT c.id, c.comment_content, c.artid, c.comment_name, c.comment_time, i.nickname, h.URL" +
            " FROM user_comments c" +
            " LEFT JOIN user_informations i" +
            " ON c.comment_uid = i.uid" +
            " LEFT JOIN user_headportrait h" +
            " ON c.comment_uid = h.uid" +
            " WHERE c.artid IN (SELECT id FROM user_articles WHERE uid = #{userId})")
    @Results(value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "comment_content", property = "content"),
            @Result(column = "artid", property = "parentId"),
            @Result(column = "comment_time", property = "commentTime"),
            @Result(column = "comment_name", property = "fromUserName"),
            @Result(column = "nickname", property = "fromNickname"),
            @Result(column = "URL", property = "fromUrl")
    })
    List<AdviceComment> findReceiveCommentByUserId(String userId);

    // 获取当前用户所有收到回复的信息
    @Select("SELECT r.id, r.comment_id, r.reply_content, r.reply_time, r.reply_from_name, u.nickname, h.URL" +
            " FROM user_replys r" +
            " LEFT JOIN user_informations u" +
            " ON r.reply_from_uid = u.uid" +
            " LEFT JOIN user_headportrait h" +
            " ON r.reply_from_uid = h.uid" +
            " WHERE r.reply_to_uid = #{userId}")
    @Results(value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "reply_content", property = "content"),
            @Result(column = "comment_id", property = "parentId"),
            @Result(column = "reply_time", property = "commentTime"),
            @Result(column = "reply_from_name", property = "fromUserName"),
            @Result(column = "nickname", property = "fromNickname"),
            @Result(column = "URL", property = "fromUrl")
    })
    List<AdviceComment> findReceiveReplyByUserId(String userId);

    // 获取指定评价id消息推送缓存的数据
    @Select("SELECT c.id, c.artid, c.comment_content, FROM_UNIXTIME(c.comment_time/1000,'%Y-%m-%d %H:%i:%s') as comment_time, a.uid AS toUid, u.username AS toUsername, i.nickname, h.URL FROM user_comments c" +
            " LEFT JOIN user_articles a" +
            " ON c.artid = a.id" +
            " LEFT JOIN user_informations i" +
            " ON c.comment_uid = i.uid" +
            " LEFT JOIN user_headportrait h" +
            " ON c.comment_uid = h.uid" +
            " LEFT JOIN users u" +
            " ON a.uid = u.id" +
            " WHERE c.id = #{commentId}")
    @Results(value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "comment_content", property = "content"),
            @Result(column = "artid", property = "parentId"),
            @Result(column = "comment_time", property = "commentTime"),
            @Result(column = "nickname", property = "fromNickname"),
            @Result(column = "URL", property = "fromUrl"),
            @Result(column = "toUid", property = "toUid"),
            @Result(column = "toUsername", property = "toUserName")
    })
    AdviceComment findAdviceCommentBycommentId(Integer commentId);

    //获取指定回复id消息推送缓存的数据
    @Select("SELECT r.id, c.artid AS artid, FROM_UNIXTIME(r.reply_time/1000,'%Y-%m-%d %H:%i:%s') as reply_time, i.nickname AS fromNickname, h.URL AS fromUrl" +
            " FROM user_replys r" +
            " LEFT JOIN user_informations i" +
            " ON r.reply_from_uid = i.uid" +
            " LEFT JOIN user_headportrait h" +
            " ON  r.reply_from_uid = h.uid" +
            " LEFT JOIN user_comments c" +
            " ON r.comment_id = c.id" +
            " WHERE r.id = #{replyId}")
    @Results(value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "artid", property = "parentId"),
            @Result(column = "reply_time", property = "commentTime"),
            @Result(column = "fromNickname", property = "fromNickname"),
            @Result(column = "fromUrl", property = "fromUrl"),
    })
    AdviceComment findAdviceReplyByreplyId(Integer replyId);
}
